/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// d3d_draw2d.cpp - 2d rendering stuff


#include "d3d_local.h"


uint Vec4ToByte4(const EVec4 &v)
{
	EVec4 cv = v;
	cv.x = Clamp<float>(cv.x, 0, 1);
	cv.y = Clamp<float>(cv.y, 0, 1);
	cv.z = Clamp<float>(cv.z, 0, 1);
	cv.w = Clamp<float>(cv.w, 0, 1);
	uchar r = (uchar)(255 * cv.x);
	uchar g = (uchar)(255 * cv.y);
	uchar b = (uchar)(255 * cv.z);
	uchar a = (uchar)(255 * cv.w);
	
	return D3DCOLOR_ARGB(a,r,g,b);
}


struct vertex2d_hw_s {
		float	 x, y, z;
		uint32_t color;
		float	 u, v;
	};



//
//	ED3DRenderSystem::Set2DColor
//
void ED3DRenderSystem::Set2DColor(const EVec4 &color)
{
	color_2d_multiplier	=	color;
}


void ED3DRenderSystem::Set2DBlendMode(ERSBlendMode_t mode)
{
	if (mode==RS_BLEND_NORMAL) {
		HRCALL( d3ddev->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA) );
		HRCALL( d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA) );
	}
	if (mode==RS_BLEND_ADDITIVE) {
		HRCALL( d3ddev->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_ONE) );
		HRCALL( d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE) );
	}
	if (mode==RS_BLEND_LIGHT) {
		HRCALL( d3ddev->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_DESTCOLOR) );
		HRCALL( d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE) );
	}
}


//
//	ED3DRenderSystem::SetTransform
//
void ED3DRenderSystem::SetTransform(const EMatrix4 &matrix)
{
	D3DXMATRIX	M;
	//memcpy(&M, &Matrix4Transpose(matrix), sizeof(M));
	memcpy(&M, &matrix, sizeof(M));
	d3ddev->SetTransform(D3DTS_VIEW, &M);
}


//
//	ED3DRenderSystem::SetProjection
//
void ED3DRenderSystem::SetProjection(const EMatrix4 &matrix)
{
	D3DXMATRIX	M;
	//memcpy(&M, &Matrix4Transpose(matrix), sizeof(M));
	memcpy(&M, &matrix, sizeof(M));
	d3ddev->SetTransform(D3DTS_PROJECTION, &M);
}


//
//	ED3DRenderSystem::SetViewPort
//
void ED3DRenderSystem::SetViewport(int x, int y, int w, int h)
{
	uint sw, sh;
	GetScreenSize(sw, sh);

	D3DVIEWPORT9	vp;
	if (x==y==w==h) {
		vp.X      = 0;
		vp.Y      = 0;
		vp.Width  = sw;
		vp.Height = sh;
		vp.MinZ   = 0.0f;
		vp.MaxZ   = 1.0f;
	} else {
		vp.X      = x;
		vp.Y      = y;
		vp.Width  = w;
		vp.Height = h;
		vp.MinZ   = 0.0f;
		vp.MaxZ   = 1.0f;
	}
	
	HRCALL( d3ddev->SetViewport(&vp) );
}


//
//	ED3DRenderSystem::Set2DViewMode
//
void ED3DRenderSystem::Set2DViewMode(float offset_x/* =0 */, float offset_y/* =0  */)
{
	uint w, h;
	GetScreenSize(w, h);

	float fw = (float)w;
	float fh = (float)h;
	float offset = 0.5f;
	
	//	Set 2D projection matrix :
	D3DXMATRIX	wrld;
	D3DXMATRIX	view;
	D3DXMATRIX	proj;
	D3DXMatrixIdentity(&view);
	D3DXMatrixIdentity(&wrld);
    D3DXMatrixOrthoOffCenterLH(&proj, 0+offset - offset_x, fw+offset - offset_x, fh+offset - offset_y, 0+offset - offset_y, -1, +1);
	//D3DXMatrixOrthoOffCenterLH(&proj, 0+offset - offset_x, fw+offset - offset_x, fh+offset - offset_y, 0+offset - offset_y, -1, +1);
	//D3DXMatrixOrthoOffCenterLH(&proj, 0, fw, 0, fh, -1, +1);
	//D3DXMatrixOrthoOffCenterLH(&proj, -fw/2, fw/2, -fh/2, fh/2, -1, +1);
	d3ddev->SetTransform(D3DTS_WORLD,		&wrld);
	d3ddev->SetTransform(D3DTS_VIEW,		&view);
	d3ddev->SetTransform(D3DTS_PROJECTION,	&proj);
	
	//	Set appropriate blending mode :
	HRCALL( d3ddev->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE) );
	HRCALL( d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE) );
	HRCALL( d3ddev->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA) );
	HRCALL( d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA) );
	HRCALL(	d3ddev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR) );
	HRCALL( d3ddev->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR) );
	HRCALL( d3ddev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR) );
	
	HRCALL( d3ddev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE) );
	HRCALL( d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE) );
}


//
//	ED3DRenderSystem::Set2DViewMode
//
void ED3DRenderSystem::SetCustom2DViewMode(const EVec2 &upper_left, const EVec2 &lower_right)
{
	Set2DViewMode();	// setup same render states :

	
	float l,r,b,t;
	l = upper_left.x;
	r = lower_right.x;
	t = upper_left.y;
	b = lower_right.y;
	
	//	Set 2D projection matrix :
	D3DXMATRIX	wrld;
	D3DXMATRIX	view;
	D3DXMATRIX	proj;
	D3DXMatrixIdentity(&view);
	D3DXMatrixIdentity(&wrld);
	D3DXMatrixOrthoOffCenterLH(&proj, l, r, b, t, -1, +1);
	d3ddev->SetTransform(D3DTS_WORLD,		&wrld);
	d3ddev->SetTransform(D3DTS_VIEW,		&view);
	d3ddev->SetTransform(D3DTS_PROJECTION,	&proj);
}


//
//	ED3DRenderSystem::DrawQuad
//
void ED3DRenderSystem::DrawQuad(float x0, float y0, float x1, float y1, 
								float u0, float v0, float u1, float v1, uint32_t color4b)
{
	vertex2d_hw_s quad[4];

#if 0
//	SetSampler(0, NULL);
	HRCALL( d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME) );
#endif	
	
	quad[0].x = x0;	quad[0].y = y0;	quad[0].z = 0;
	quad[0].u = u0; quad[0].v = v0; quad[0].color = color4b;

	quad[1].x = x1;	quad[1].y = y0;	quad[1].z = 0;
	quad[1].u = u1; quad[1].v = v0; quad[1].color = color4b;

	quad[2].x = x1;	quad[2].y = y1;	quad[2].z = 0;
	quad[2].u = u1; quad[2].v = v1; quad[2].color = color4b;

	quad[3].x = x0;	quad[3].y = y1;	quad[3].z = 0;
	quad[3].u = u0; quad[3].v = v1; quad[3].color = color4b;
	
	HRCALL( d3ddev->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) );
	
	HRCALL( d3ddev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &quad, sizeof(vertex2d_hw_s)) );
}


//
//	ED3DRenderSystem::DrawQuad
//
void ED3DRenderSystem::DrawPolygon(uint vert_num, const vert2d_s *vert_list, const char *tex_name)
{
	ASSERT(vert_num>2);
	ASSERT(vert_num<=32);
	vertex2d_hw_s p[32];

	IImage *image = RegisterImage(tex_name);
	SetSampler(0, image);

	for (uint i=0; i<vert_num; i++)	{
		p[i].color = Vec4ToByte4(vert_list[i].color * color_2d_multiplier);
		p[i].u     = vert_list[i].uv.x;
		p[i].v     = vert_list[i].uv.y;
		p[i].x     = vert_list[i].p.x;
		p[i].y     = vert_list[i].p.y;
		p[i].z     = 0;
	}
	
	HRCALL( d3ddev->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) );
	
	HRCALL( d3ddev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, vert_num-2, &p, sizeof(vertex2d_hw_s)) );
}


//
//  
//
void ED3DRenderSystem::DrawQuadNoTex(float x0, float y0, float x1, float y1, 
									 uint32_t color4b)
{
	vertex2d_hw_s quad[4];

#if 0
//	SetSampler(0, NULL);
	HRCALL( d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME) );
#endif	
	
	quad[0].x = x0;	quad[0].y = y0;	quad[0].z = 0;
	quad[0].color = color4b;

	quad[1].x = x1;	quad[1].y = y0;	quad[1].z = 0;
	quad[1].color = color4b;

	quad[2].x = x1;	quad[2].y = y1;	quad[2].z = 0;
	quad[2].color = color4b;

	quad[3].x = x0;	quad[3].y = y1;	quad[3].z = 0;
	quad[3].color = color4b;
	
	HRCALL( d3ddev->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE) );
	
	HRCALL( d3ddev->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &quad, sizeof(vertex2d_hw_s)) );
}

//
//	ED3DRenderSystem::DrawImage
//
void ED3DRenderSystem::DrawImage(float x, float y, const char *name, const EVec4 color)
{
	IImage *image = RegisterImage(name);

	SetSampler(0, image);
	
	float x0 = (float)(x);
	float x1 = (float)(x + image->GetWidth());
	float y0 = (float)(y);
	float y1 = (float)(y + image->GetHeight());
	
	DrawQuad(x0, y0, x1, y1, 0,0, 1,1, Vec4ToByte4(color * color_2d_multiplier));
}


//
//	ED3DRenderSystem::DrawImage
//
void ED3DRenderSystem::DrawStretchImage(float x, float y, float w, float h, const char *name, const EVec4 color)
{
	IImage *image = RegisterImage(name);

	SetSampler(0, image);
	
	float x0 = (float)(x);
	float x1 = (float)(x + w);
	float y0 = (float)(y);
	float y1 = (float)(y + h);
	
	DrawQuad(x0, y0, x1, y1, 0,0, 1,1, Vec4ToByte4(color * color_2d_multiplier));
}


//
//	ED3DRenderSystem::DrawQuadList
//	- draw series of quads. very useful function for UI drawing
//
void ED3DRenderSystem::DrawQuadList(uint quad_num, const quad_s *quad_list, const char *tex_name)
{
	IImage *image = RegisterImage(tex_name);
	SetSampler(0, image);
	
	for (uint i=0; i<quad_num; i++) 
	{
		const quad_s *q = &quad_list[i];
		
		EVec4	color = q->color * color_2d_multiplier;
	
		DrawQuad( q->p0.x,   q->p0.y,    q->p1.x,  q->p1.y,  
				  q->uv0.x,	 q->uv0.y,   q->uv1.x, q->uv1.y, Vec4ToByte4(q->color * color_2d_multiplier) );
	}
}



void ED3DRenderSystem::DrawRectOutline(float x, float y, float w, float h, const EVec4 &color)
{
	vertex2d_hw_s quad[5];
	memset(quad, 0, sizeof(quad));
	
	uint color4b = Vec4ToByte4(color);
	
	float x0 = x;
	float y0 = y;
	float x1 = x + w;
	float y1 = y + h;

	quad[0].x = x0;	quad[0].y = y0;	quad[0].z = 0; quad[0].color = color4b;
	quad[1].x = x1;	quad[1].y = y0;	quad[1].z = 0; quad[1].color = color4b;
	quad[2].x = x1;	quad[2].y = y1;	quad[2].z = 0; quad[2].color = color4b;
	quad[3].x = x0;	quad[3].y = y1;	quad[3].z = 0; quad[3].color = color4b;
	quad[4].x = x0;	quad[4].y = y0;	quad[4].z = 0; quad[4].color = color4b;
	
	SetSampler(0, NULL);

	HRCALL( d3ddev->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) );
	HRCALL( d3ddev->DrawPrimitiveUP(D3DPT_LINESTRIP, 4, &quad, sizeof(vertex2d_hw_s)) );
}





